package nz.co.listit.hailacabprototype;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.ProgressDialog;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class HailACabPrototypeActivity extends ListItResClient implements
		SensorEventListener {
	private TextView _tvAzimuth;
	private TextView _tvPitch;
	private TextView _tvRoll;
	private TextView _tvCoord;
	private double _lat, _long;
	private double _azimuth;

	private SensorManager _sensorManager;
	private Sensor _accelerometer;

	private LocationManager _locationManager;
	private LocationListener _locationListener = new LocationListener() {

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		public void onLocationChanged(Location location) {
			_lat = location.getLatitude();
			_long = location.getLongitude();

			_tvCoord.setText(String.format("Lat:%f, Long:%f",
					location.getLatitude(), location.getLongitude()));
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		_sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		_accelerometer = _sensorManager
				.getDefaultSensor(Sensor.TYPE_ORIENTATION);

		// setup location manager to get updates to user location
		_locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);

		_tvAzimuth = (TextView) findViewById(R.id.tvAzimuth);
		_tvPitch = (TextView) findViewById(R.id.tvPitch);
		_tvRoll = (TextView) findViewById(R.id.tvRoll);
		_tvCoord = (TextView) findViewById(R.id.tvCoordinate);
	}

	@Override
	protected void onResume() {
		super.onResume();
		_sensorManager.registerListener(this, _accelerometer,
				SensorManager.SENSOR_DELAY_NORMAL);

		_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				0, 0, _locationListener);

		_locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, _locationListener);
	}

	@Override
	protected void onPause() {
		super.onPause();
		_sensorManager.unregisterListener(this);
		_locationManager.removeUpdates(_locationListener);
	}

	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	public void onSensorChanged(SensorEvent event) {
		// alpha is calculated as t / (t + dT)
		// with t, the low-pass filter's time-constant
		// and dT, the event delivery rate

		// final float alpha = (float) 0.8;

		// float[] gravity;
		_azimuth = event.values[0];
		_tvAzimuth.setText(String.format("Azimuth: %f", event.values[0]));
		_tvPitch.setText(String.format("Pitch: %f", event.values[1]));
		_tvRoll.setText(String.format("Roll: %f", event.values[2]));

		// linear_acceleration[0] = event.values[0] - gravity[0];
		// linear_acceleration[1] = event.values[1] - gravity[1];
		// linear_acceleration[2] = event.values[2] - gravity[2];
	}

	public void onHailClick(View v) {
		// the progress dlg need to be invoked here and not inside the
		// thread below
		//HailACabPrototypeActivity.this._progDlg = ProgressDialog.show(HailACabPrototypeActivity.this, "",
		//		"Please wait...", true);

		// now login to List-It server
		Thread thread = new Thread(HailACabPrototypeActivity.this);
		thread.start();
	}

	@Override
	public void run() {
		ListItServerClient ltClient = new ListItServerClient();

		List<NameValuePair> qparams = new ArrayList<NameValuePair>();

		Message msg = new Message();

		// add device ID
		qparams.add(new BasicNameValuePair("deviceID", this.getDeviceID()));

		// add username
		qparams.add(new BasicNameValuePair("latitude", String.valueOf(_lat)));

		// latitude
		qparams.add(new BasicNameValuePair("longitude", String.valueOf(_long)));

		// azimuth
		qparams.add(new BasicNameValuePair("azimuth", String.valueOf(_azimuth)));

		// call web service
		ltClient.queryListItService("HailCab", qparams);
		//HailACabPrototypeActivity.this._progDlg.hide();
	}
}